package nsu.edu.zsq.controller.sys;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import nsu.edu.zsq.bean.SchOrg;
import nsu.edu.zsq.common.JsonData;
import nsu.edu.zsq.exception.ParamException;
import nsu.edu.zsq.service.sys.SchOrgService;
import nsu.edu.zsq.util.SchOrgUtil;

/** 
* Description: 学校组织（院系）的controller类<br>
*/
@Controller
@RequestMapping("/sys/school/org")
public class SchOrgController {

    @Autowired
    private SchOrgService schOrgService;
    
    /**
     * 获取组织列表信息，带有“层级结构、组织层级、是否有子组织”信息
     * @return JsonData
     */
    @RequestMapping("/listSchOrg") 
    @ResponseBody
    public JsonData listSchOrgWithLevel() {
        List<SchOrg> schOrgs = schOrgService.listSchOrg();
        List<SchOrg> sortedSchOrgs = SchOrgUtil.sortByParentOrgId(schOrgs);
        List<SchOrg> soredSchOrgsWithLevel = SchOrgUtil.setSchOrgLevel(sortedSchOrgs);
        return JsonData.success(soredSchOrgsWithLevel);
    }
    
    /**
     * 保存一条组织信息
     * @param schOrg 组织信息
     * @return JsonData
     */
    @RequestMapping("/saveSchOrg")
    @ResponseBody
    public JsonData saveSchOrg(SchOrg schOrg) {
        schOrgService.saveSchOrg(schOrg);
        return JsonData.success();
    }
    
    /**
     * 跳转到添加子组织的页面
     * @param model 向页面传数据
     * @param orgId 要添加子组织的组织id
     * @return String manager/school/addOrgChild.html
     */
    @RequestMapping("/toAddOrgChild")
    public String toAddOrgChild(Model model, @RequestParam("orgId") int orgId) {
        model.addAttribute("orgId", orgId);
        return "manager/school/addOrgChild.html";
    }
    
    /**
     * 跳转到编辑组织的页面
     * @param model 向页面传数据
     * @param orgId 组织id
     * @return Stringmanager/school/editOrg.html
     */
    @RequestMapping("/toEditOrg")
    public String toEditOrg(Model model, @RequestParam("orgId") int orgId) {
        SchOrg schOrg = schOrgService.getSchOrg(orgId);
        model.addAttribute("schOrg", schOrg);
        return "manager/school/editOrg.html";
    }
    
    /**
     * 更新一条院系信息
     * @param schOrg
     * @return JsonData
     */
    @RequestMapping("/updateSchOrg")
    @ResponseBody
    public JsonData updateSchOrg(SchOrg schOrg) {
        schOrgService.updateSchOrg(schOrg);
        return JsonData.success();
    }
    
    /**
     * 单个或者批量删除组织
     * @param orgId 组织id数组
     * @return JsonData
     */
    @RequestMapping("/delSchOrg")
    @ResponseBody
    public JsonData delSchOrg(@RequestParam("orgId[]")int orgId[]) {
        if (orgId.length > 1) {
            // 批量删除
            schOrgService.deleteBatchSchOrg(orgId);
        } else if (orgId.length == 1) {
            // 单个删除
            schOrgService.deleteSchOrg(orgId[0]);
        } else {
            throw new ParamException("orgId无参数");
        }
        return JsonData.success();
    }
}
